package com.flycat.service.impl;

import com.flycat.common.enumeration.ServiceTypeEnum;
import com.flycat.common.enumeration.StatusEnum;
import com.flycat.common.exception.BizException;
import com.flycat.common.util.JsonUtils;
import com.flycat.core.AbstractService;
import com.flycat.dao.mapper.ProductCarMapper;
import com.flycat.dao.mapper.ProductInfoMapper;
import com.flycat.dao.po.CarModels;
import com.flycat.dao.po.ProductCar;
import com.flycat.dao.po.ProductInfo;
import com.flycat.dto.*;
import com.flycat.dto.query.ProductInfoQuery;
import com.flycat.service.CarModelsService;
import com.flycat.service.ProductCarService;
import com.flycat.service.ProductInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by yalei.chen on 2017/09/16.
 */
@Service
@Transactional
public class ProductInfoServiceImpl extends AbstractService<ProductInfo> implements ProductInfoService {

    private static final Logger logger = LoggerFactory.getLogger(ProductInfoServiceImpl.class);


    @Resource
    private ProductInfoMapper productInfoMapper;

    @Resource
    private ProductCarService productCarService;

    @Resource
    private CarModelsService carModelsService;

    @Override
    public List<CarProductInfo> queryCarList(ProductInfoQuery productInfoQuery) {
        List<CarProductInfo> result =  productInfoMapper.queryCarList(productInfoQuery);
        result.stream().forEach(item->{
            item.setServiceType(item.getServiceType());
            item.setStatusMsg(StatusEnum.getEnum(item.getStatus()).msg);
            item.setCarModelNum(item.getCarModelNum() + "种");
        });
        return result;
    }

    @Transactional
    @Override
    public int saveCar(CarProductSave carProductSave) {
        List<CarProductModel> carProductModels = carProductSave.getCarProductModels();
        if (CollectionUtils.isEmpty(carProductModels)) {
            throw new BizException("车辆价格信息不准为空");
        }
        try {
            ProductInfo productInfo = new ProductInfo();
            productInfo.setServiceType(carProductSave.getServiceType());
            productInfo.setType(Byte.valueOf(carProductSave.getProductType()));
            productInfo.setCityId(carProductSave.getCityId());
            productInfo.setCityName(carProductSave.getCityName());
            productInfo.setDepPlace(carProductSave.getDepPlace());
            productInfo.setArrPlace(carProductSave.getArrPlace());
            productInfo.setStatus(StatusEnum.OFF_LINE.status);
            save(productInfo);
            List<ProductCar> productCarList = carProductModels.stream().map(item -> {
                ProductCar productCar = new ProductCar();
                productCar.setCarModelId(item.getCarModelId());
                productCar.setDistorFinPrice(item.getDistorFinPrice());
                productCar.setProductId(productInfo.getId());
                productCar.setSupperFinPrice(item.getSupperFinPrice());
                productCar.setSalePrice(item.getSalePrice());
                productCar.setStartPrice(item.getStartPrice());
                productCar.setUpStartPrice(item.getUpStartPrice());
                return productCar;
            }).collect(Collectors.toList());
            productCarService.save(productCarList);
            return 1;
        } catch (Exception e) {
            logger.error("存储商品信息发生未知异常", e);
            return 0;
        }
    }

    @Override
    public int deleteCar(ProductInfoQuery productInfoQuery) {
        try {
            deleteById(Integer.valueOf(productInfoQuery.getProductId()));
            Condition condition = new Condition(ProductCar.class);
            condition.createCriteria().andEqualTo("productId", productInfoQuery.getProductId());
            productCarService.deleteByCondition(condition);
            return 1;
        } catch (Exception e) {
            logger.error("删除商品时发生异常",e);
            return 0;
        }
    }

    @Override
    public int updateCar(CarProductSave carProductSave) {
        try {
            ProductInfo productInfo = new ProductInfo();
            productInfo.setServiceType(carProductSave.getServiceType());
            productInfo.setType(Byte.valueOf(carProductSave.getProductType()));
            productInfo.setCityId(carProductSave.getCityId());
            productInfo.setCityName(carProductSave.getCityName());
            productInfo.setDepPlace(carProductSave.getDepPlace());
            productInfo.setArrPlace(carProductSave.getArrPlace());
            update(productInfo);

            List<CarProductModel> carProductModels = carProductSave.getCarProductModels();
            if (CollectionUtils.isEmpty(carProductModels)) {
                logger.info("不需要更改价格信息,req:{}", JsonUtils.toJson(carProductSave));
            }
            carProductModels.stream().map(item -> {
                ProductCar productCar = new ProductCar();
                productCar.setId(Integer.valueOf(item.getId()));
                productCar.setCarModelId(item.getCarModelId());
                productCar.setDistorFinPrice(item.getDistorFinPrice());
                productCar.setSupperFinPrice(item.getSupperFinPrice());
                productCar.setSalePrice(item.getSalePrice());
                productCar.setStartPrice(item.getStartPrice());
                productCar.setUpStartPrice(item.getUpStartPrice());
                return productCar;
            }).forEach(productCarService::update);
            return 1;
        } catch (Exception e) {
            logger.error("更新价格信息时发生异常 req:{}", JsonUtils.toJson(carProductSave), e);
            return 0;
        }


    }

    @Override
    public CarProductSave qeuryByProductId(ProductInfoQuery productInfoQuery) {
        try {
            CarProductSave carProductSave = new CarProductSave();
            ProductInfo productInfo = findById(Integer.valueOf(productInfoQuery.getProductId()));
            carProductSave.setProductType(String.valueOf(productInfo.getType()));
            carProductSave.setArrPlace(productInfo.getArrPlace());
            carProductSave.setCityId(productInfo.getCityId());
            carProductSave.setCityName(productInfo.getCityName());
            carProductSave.setDepPlace(productInfo.getDepPlace());
            carProductSave.setStation(productInfo.getStation());
            carProductSave.setServiceType(productInfo.getServiceType());
            Condition condition = new Condition(ProductCar.class);
            condition.createCriteria().andEqualTo("productId", productInfoQuery.getProductId());
            List<ProductCar> productCars = productCarService.findByCondition(condition);
            List<CarProductModel> carProductModels = productCars.stream().map(item->{
                CarProductModel carProductModel = new CarProductModel();
                carProductModel.setSalePrice(item.getSalePrice());
                carProductModel.setCarModelId(item.getCarModelId());
                carProductModel.setDistorFinPrice(item.getDistorFinPrice());
                carProductModel.setId(String.valueOf(item.getId()));
                carProductModel.setProductId(String.valueOf(item.getProductId()));
                carProductModel.setSupperFinPrice(item.getSupperFinPrice());
                CarModels carModels = carModelsService.findById(Integer.valueOf(item.getCarModelId()));
                if (carModels != null) {
                    carProductModel.setBrand(carModels.getBrand());
                    carProductModel.setType(carModels.getType());
                    carProductModel.setSeatNum(carModels.getSeatNum());
                    carProductModel.setOutputVolume(carModels.getOutputVolume());
                    carProductModel.setModel(carModels.getModel());
                }

                return carProductModel;
            }).collect(Collectors.toList());
            carProductSave.setCarProductModels(carProductModels);
            return carProductSave;
        } catch (Exception e) {
            logger.error("查询商品信息时发生异常,req:{}", JsonUtils.toJson(productInfoQuery), e);
            return null;
        }

    }

}
